package com.jsz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jsz.constant.MessageConstant;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.entity.Result;
import com.jsz.pojo.CheckItem;
import com.jsz.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查项管理
 */
//@Controller
//@ResponseBody
@RestController   //这个注解能替换掉上面注释的2个注解
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    //添加数据
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//添加数据权限
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            //调用业务层操作数据
            checkItemService.addCheckItem(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    //根据条件分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用业务层操作数据
        PageResult pageResult = checkItemService.findPage(queryPageBean);

        return pageResult;
    }

    //根据ID查询数据
    @RequestMapping("/findById/{id}")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//弹出编辑窗口权限
    public Result findById(@PathVariable Integer id) {
        try {
            //调用业务层操作数据
            CheckItem checkItem = checkItemService.findById(id);
            //查询成功,储存成功消息
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //根据ID删除用户信息
    @RequestMapping("/deleteById/{id}")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//删除数据权限
    public Result deleteById(@PathVariable Integer id) {
        try {
            //调用业务层操作数据
            checkItemService.deleteById(id);
        } catch (RuntimeException r) {
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            //删除数据失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        //删除数据成功
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //编辑数据
    @RequestMapping("/edit")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")//编辑框修改数据权限
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            //调用业务层操作数据
            checkItemService.edit(checkItem);
            //查询成功,储存成功消息
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    //查询所有checkItem信息
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            //调用业务层操作数据
            List<CheckItem> list = checkItemService.findAll();
            //查询数据成功,回显成功的消息
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            //查询数据失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}